<?php

namespace App\Console\Commands;

use Doctrine\Common\Cache\Cache;
use Illuminate\Console\Command;
use QL\QueryList;

class SuanliCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'suanli';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取实时算力';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $ql = QueryList::get('https://m.btc.com/stats/diff');
        $aa = $ql->find('.diff-summary>ul>li>dl>dd')->texts();
        $aa = json_decode($aa,true);
        $Hashrate = $this->trimall($aa[0]);
        $Difficulty = $this->trimall($aa[1]);
        $NextDifficultyEstimated = $this->trimall($aa[2]);
        $DatetoNextDifficulty = $this->trimall($aa[3]);
        $suanli = [
            'Hashrate' => $Hashrate,
            'Difficulty' => $Difficulty,
            'NextDifficultyEstimated' => $NextDifficultyEstimated,
            'DatetoNextDifficulty' => $DatetoNextDifficulty
        ];
        \Cache::put('suanli', $suanli,now()->addMinutes(60));
        
    }

    function trimall($str)//删除空格
    {
        $qian=array(" ","　","\t","\n","\r");
        $hou=array("","","","","");
        return str_replace($qian,$hou,$str);
    }
}
